/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.ui;

import java.awt.*;
import javax.swing.*;

import sk.jp.commons.tools.Messages;
import sk.jp.commons.utils.StringUtils;

/**
 * Simple warning dialog to present text message.
 */
public class WarningDialog {

    public enum Msg {
        dlgTitle("ui.warndialog.title"),//Warning
        btnClose("ui.warndialog.btn.close"),//OK
        ;
        //----------------------------------------------------------------------
        private String propKey;
        Msg(String propKey) {
            this.propKey = propKey;
        }
        public String getPropKey() {
            return propKey;
        }
        public String value() {
            return Messages.getMessage(propKey);
        }
    }

    //----------------------------------------------------------------------

    private final String title;
    private final String[] options;

    //----------------------------------------------------------------------

    /**
     * Initialize dialog with default properties.
     */
    public WarningDialog() {
        this(Msg.dlgTitle.value(), Msg.btnClose.value());
    }

    /**
     * Initialize dialog with specified title and close button label.
     * @param title
     * @param btnCloseLabel
     */
    public WarningDialog(String title, String btnCloseLabel) {
        this.title = title;
        this.options = new String[]{btnCloseLabel};
    }

    //----------------------------------------------------------------------

    /**
     * Presents given text.
     * @param ownerFrame parent component.
     */
    public void show(Component ownerFrame, String text){
        text = wrapText(text);
        JOptionPane.showOptionDialog(ownerFrame, text, title,
                                     JOptionPane.OK_OPTION,
                                     JOptionPane.WARNING_MESSAGE,
                                     null, options, null);
    }

    //----------------------------------------------------------------------

    private static String wrapText(String text){
        return StringUtils.wrapText(text, 80, StringUtils.SPLIT_EXT, "\n");
    }

}